﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using SharedGenomics.Annotations;
using SharedGenomics.Workbench.Annotations;
using SharedGenomics.Workbench.Annotations.Requests;
using SharedGenomics.Workbench.Tests.TestObjects;

using Rhino.Mocks;

namespace SharedGenomics.Workbench.Tests.Annotations.Requests
{
    [TestClass]
    public class PubMedAnnotationRequestStageTests
    {
        private MockRepository mocks;
        private PubMedAnnotationRequestStage stage;
        private IAnnotationRepositoryMediator annotations;

        [TestInitialize]
        public void Setup()
        {
            this.mocks = new MockRepository();

            this.annotations = this.mocks.Stub<IAnnotationRepositoryMediator>();
            this.stage = new PubMedAnnotationRequestStage(this.annotations);
        }

        [TestMethod]
        public void ShouldCreateAnnotation()
        {
            this.annotations.Stub(ar => ar.GetAnnotationForTarget(AnnotationKey.EntrezGeneId, "rs1")).Return(1234);
            this.mocks.ReplayAll();

            var context = new AnnotationRequestContext(AnnotationKey.PubMedSearch, "rs1");
            var traversalStrategy = new TestAnnotationRequestPipelineTraversalStrategy();

            this.stage.Process(context, traversalStrategy);
            var result = context.Results.First();

            PubMedSearchAnnotation pmAnnotation = (PubMedSearchAnnotation)result.Annotation;

            Assert.AreEqual(1234, pmAnnotation.EntrezGeneId);
            Assert.IsTrue(traversalStrategy.MovedNext);
        }

        [TestMethod]
        public void ShouldFailAnnotation()
        {
            this.annotations.Stub(ar => ar.GetAnnotationForTarget(AnnotationKey.EntrezGeneId, "rs1")).Return(null);
            this.mocks.ReplayAll();

            var context = new AnnotationRequestContext(AnnotationKey.PubMedSearch, "rs1");
            var traversalStrategy = new TestAnnotationRequestPipelineTraversalStrategy();

            this.stage.Process(context, traversalStrategy);
            var result = context.Results.First();

            Assert.AreEqual(AnnotationRequestStatus.Failed, result.Status);
            Assert.IsTrue(traversalStrategy.MovedNext);
        }
    }
}
